package com.sjh.baseui.widget;

import com.google.android.material.appbar.AppBarLayout;

/**
 * AppBarLayout滑动监听
 * @author Extends
 * @date 2019/10/23/023
 */
public abstract class BaseAppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
    public enum State {
        /**
         * 展开状态
         */
        EXPANDED,
        /**
         * 折叠状态
         */
        COLLAPSED,
        /**
         * 中间状态
         */
        IDLE
    }

    private State mCurrentState = State.IDLE;

    @Override
    public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
        if (i == 0) {
            if (mCurrentState != State.EXPANDED) {
                if(Math.abs(i) >= appBarLayout.getTotalScrollRange()){
                    onStateChanged(appBarLayout, State.COLLAPSED);
                    mCurrentState = State.COLLAPSED;
                    return;
                }else{
                    onStateChanged(appBarLayout, State.EXPANDED);
                }
            }
            mCurrentState = State.EXPANDED;
        } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
            if (mCurrentState != State.COLLAPSED) {
                onStateChanged(appBarLayout, State.COLLAPSED);
            }
            mCurrentState = State.COLLAPSED;
        } else {
            if (mCurrentState != State.IDLE) {
                onStateChanged(appBarLayout, State.IDLE);
            }
            mCurrentState = State.IDLE;
        }
    }

    public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}
